<template>
  <div class="login-container">
    <div class="login-card">
      <div class="login-header">
        <h2>用户登录</h2>
        <p>请输入您的登录信息</p>
      </div>
      
      <el-form
        ref="loginFormRef"
        :model="loginForm"
        :rules="loginRules"
        class="login-form"
        size="large"
        @submit.prevent="handleLogin"
      >
        <el-form-item prop="username">
          <el-input
            v-model="loginForm.username"
            placeholder="请输入登录名"
            prefix-icon="User"
            clearable
          />
        </el-form-item>
        
        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            type="password"
            placeholder="请输入密码"
            prefix-icon="Lock"
            show-password
            clearable
          />
        </el-form-item>
        
        <el-form-item>
          <div class="button-group">
            <el-button
              type="primary"
              class="login-button"
              :loading="loading"
              @click="handleLogin"
            >
              {{ loading ? '登录中...' : '登录' }}
            </el-button>
            
            <el-button
              class="reset-button"
              @click="handleReset"
            >
              重置
            </el-button>
          </div>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script setup lang="ts">
import { reactive, ref } from 'vue'
import { ElMessage, type FormInstance, type FormRules } from 'element-plus'
import { useRouter } from 'vue-router'

// 路由实例
const router = useRouter()

// 表单引用
const loginFormRef = ref<FormInstance>()

// 加载状态
const loading = ref(false)

// 表单数据
const loginForm = reactive({
  username: '',
  password: ''
})

// 表单验证规则
const loginRules: FormRules = {
  username: [
    { required: true, message: '请输入登录名', trigger: 'blur' },
    { min: 2, max: 20, message: '登录名长度应在 2 到 20 个字符之间', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 20, message: '密码长度应在 6 到 20 个字符之间', trigger: 'blur' }
  ]
}

// 登录处理函数
const handleLogin = async () => {
  if (!loginFormRef.value) return
  
  try {
    // 表单验证
    await loginFormRef.value.validate()
    
    loading.value = true
    
    // 模拟登录请求（这里可以替换为实际的登录 API 调用）
    setTimeout(() => {
      loading.value = false
      
      // 登录成功提示
      ElMessage.success('登录成功！')
      
      // 跳转到首页
      router.push('/home')
    }, 1500)
    
  } catch (error) {
    console.error('表单验证失败:', error)
    ElMessage.error('请检查输入信息')
  }
}

// 重置表单
const handleReset = () => {
  if (!loginFormRef.value) return
  
  loginFormRef.value.resetFields()
  ElMessage.info('表单已重置')
}
</script>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background: 
    /* 主渐变层 - 霓虹紫青配色 */
    linear-gradient(135deg, 
      #667eea 0%, 
      #764ba2 25%, 
      #f093fb 50%, 
      #f5576c 75%, 
      #4facfe 100%
    ),
    /* 动态几何背景 */
    url('data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1200 800"><defs><radialGradient id="neon1" cx="50%" cy="50%" r="60%"><stop offset="0%" style="stop-color:%23ff00ff;stop-opacity:0.15"/><stop offset="100%" style="stop-color:%2300ffff;stop-opacity:0.05"/></radialGradient><radialGradient id="neon2" cx="50%" cy="50%" r="40%"><stop offset="0%" style="stop-color:%23ffff00;stop-opacity:0.1"/><stop offset="100%" style="stop-color:%23ff6b6b;stop-opacity:0.03"/></radialGradient><linearGradient id="wave1" x1="0%" y1="0%" x2="100%" y2="100%"><stop offset="0%" style="stop-color:%2300d4ff;stop-opacity:0.08"/><stop offset="50%" style="stop-color:%235b73ff;stop-opacity:0.12"/><stop offset="100%" style="stop-color:%23ff006e;stop-opacity:0.06"/></linearGradient></defs><rect width="100%" height="100%" fill="url(%23neon1)"/><g opacity="0.8"><ellipse cx="200" cy="150" rx="120" ry="80" fill="url(%23neon2)" transform="rotate(45 200 150)"/><ellipse cx="900" cy="200" rx="160" ry="100" fill="url(%23neon1)" transform="rotate(-30 900 200)"/><ellipse cx="300" cy="600" rx="140" ry="90" fill="url(%23neon2)" transform="rotate(60 300 600)"/><ellipse cx="800" cy="650" rx="100" ry="60" fill="url(%23neon1)" transform="rotate(-45 800 650)"/><ellipse cx="600" cy="100" rx="80" ry="50" fill="url(%23neon2)" transform="rotate(15 600 100)"/></g><g opacity="0.6"><path d="M0,400 Q200,300 400,400 T800,350 T1200,400 V800 H0 Z" fill="url(%23wave1)"/><path d="M0,600 Q300,500 600,550 T1200,600 V800 H0 Z" fill="%23667eea" opacity="0.05"/></g><g opacity="0.4"><polygon points="100,100 200,50 300,120 250,200 150,180" fill="%23f093fb" opacity="0.1"/><polygon points="800,300 950,250 1000,400 900,450 750,400" fill="%234facfe" opacity="0.08"/><polygon points="400,500 550,450 600,600 500,650 350,600" fill="%23f5576c" opacity="0.12"/></g></svg>');
  background-size: cover;
  background-position: center;
  background-attachment: fixed;
  animation: backgroundShift 20s ease-in-out infinite;
  padding: 20px;
}

/* 背景动画效果 */
@keyframes backgroundShift {
  0%, 100% { 
    background-position: 0% 50%; 
    filter: hue-rotate(0deg);
  }
  25% { 
    background-position: 25% 25%; 
    filter: hue-rotate(90deg);
  }
  50% { 
    background-position: 50% 75%; 
    filter: hue-rotate(180deg);
  }
  75% { 
    background-position: 75% 25%; 
    filter: hue-rotate(270deg);
  }
}

.login-card {
  background: 
    linear-gradient(145deg, 
      rgba(255, 255, 255, 0.25) 0%,
      rgba(255, 255, 255, 0.1) 50%,
      rgba(255, 255, 255, 0.05) 100%
    );
  border-radius: 24px;
  box-shadow: 
    0 8px 32px rgba(0, 0, 0, 0.12),
    0 2px 16px rgba(0, 0, 0, 0.08),
    inset 0 1px 0 rgba(255, 255, 255, 0.8),
    0 0 0 1px rgba(255, 255, 255, 0.2);
  padding: 40px;
  width: 100%;
  max-width: 420px;
  backdrop-filter: blur(40px) saturate(200%);
  border: 2px solid;
  border-image: linear-gradient(135deg, 
    rgba(255, 255, 255, 0.6) 0%,
    rgba(255, 255, 255, 0.2) 50%,
    rgba(255, 255, 255, 0.4) 100%
  ) 1;
  position: relative;
  overflow: hidden;
  transition: all 0.3s ease;
}

.login-card:hover {
  transform: translateY(-2px);
  box-shadow: 
    0 12px 40px rgba(0, 0, 0, 0.15),
    0 4px 20px rgba(0, 0, 0, 0.1),
    inset 0 1px 0 rgba(255, 255, 255, 0.9),
    0 0 0 1px rgba(255, 255, 255, 0.3);
}

.login-card::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 2px;
  background: linear-gradient(90deg, 
    transparent 0%, 
    rgba(255, 0, 255, 0.6) 25%, 
    rgba(0, 255, 255, 0.6) 50%, 
    rgba(255, 255, 0, 0.6) 75%, 
    transparent 100%
  );
  animation: shimmer 3s ease-in-out infinite;
}

@keyframes shimmer {
  0%, 100% { opacity: 0.6; }
  50% { opacity: 1; }
}

.login-card::after {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: 
    radial-gradient(circle at 15% 15%, rgba(255, 0, 255, 0.05) 0%, transparent 40%),
    radial-gradient(circle at 85% 20%, rgba(0, 255, 255, 0.04) 0%, transparent 40%),
    radial-gradient(circle at 20% 85%, rgba(255, 255, 0, 0.03) 0%, transparent 40%),
    radial-gradient(circle at 80% 80%, rgba(245, 87, 108, 0.04) 0%, transparent 40%);
  pointer-events: none;
  animation: glow 4s ease-in-out infinite alternate;
}

@keyframes glow {
  0% { opacity: 0.8; }
  100% { opacity: 1; }
}

.login-header {
  text-align: center;
  margin-bottom: 30px;
  position: relative;
  z-index: 1;
}

.login-header h2 {
  color: #333;
  font-size: 28px;
  font-weight: 600;
  margin-bottom: 8px;
}

.login-header p {
  color: #666;
  font-size: 14px;
  margin: 0;
}

.login-form {
  width: 100%;
  position: relative;
  z-index: 1;
}

.login-form .el-form-item {
  margin-bottom: 24px;
}

.button-group {
  display: flex;
  gap: 12px;
  width: 100%;
}

.login-button {
  flex: 2;
  height: 44px;
  font-size: 16px;
  font-weight: 500;
  border-radius: 8px;
}

.reset-button {
  flex: 1;
  height: 44px;
  font-size: 14px;
  border-radius: 8px;
}

/* 响应式设计 */
@media (max-width: 480px) {
  .login-card {
    padding: 24px;
    margin: 10px;
  }
  
  .login-header h2 {
    font-size: 24px;
  }
  
  .button-group {
    flex-direction: column;
  }
  
  .login-button,
  .reset-button {
    flex: none;
    width: 100%;
  }
}

/* 输入框样式增强 */
.login-form :deep(.el-input__wrapper) {
  border-radius: 8px;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
  transition: all 0.3s ease;
}

.login-form :deep(.el-input__wrapper:hover) {
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.15);
}

.login-form :deep(.el-input__wrapper.is-focus) {
  box-shadow: 0 2px 8px rgba(102, 126, 234, 0.3);
}

/* 按钮样式增强 */
.login-button {
  background: linear-gradient(135deg, 
    #667eea 0%, 
    #764ba2 30%, 
    #f093fb 60%, 
    #4facfe 100%
  );
  border: none;
  transition: all 0.3s ease;
  position: relative;
  overflow: hidden;
}

.login-button::before {
  content: '';
  position: absolute;
  top: 0;
  left: -100%;
  width: 100%;
  height: 100%;
  background: linear-gradient(90deg, 
    transparent, 
    rgba(255, 255, 255, 0.3), 
    transparent
  );
  transition: left 0.6s ease;
}

.login-button:hover::before {
  left: 100%;
}

.login-button:hover {
  transform: translateY(-1px);
  box-shadow: 
    0 4px 20px rgba(102, 126, 234, 0.4),
    0 2px 10px rgba(240, 147, 251, 0.3),
    0 1px 5px rgba(79, 172, 254, 0.2);
  background: linear-gradient(135deg, 
    #7c8ff0 0%, 
    #8654b8 30%, 
    #f5a3fc 60%, 
    #5fb8fe 100%
  );
}

.reset-button {
  background: #f5f7fa;
  color: #606266;
  border: 1px solid #dcdfe6;
  transition: all 0.3s ease;
}

.reset-button:hover {
  background: #ecf5ff;
  color: #409eff;
  border-color: #c6e2ff;
}
</style>
